#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char const* argv[])
{
    int fd;
    struct stat st;
    size_t size;
    void *bufp;

    if (argc != 2)
    {
        printf("usage: %s filename\n", argv[0]);
        exit(1);
    }

    fd = open(argv[1], O_RDONLY);
    if (fd < 0)
    {
        printf("open %s error\n", argv[1]);
        exit(1);
    }

    if (fstat(fd, &st) < 0)
    {
        printf("fstat error\n");
        exit(1);
    }

    size = st.st_size;
    printf("size of %s is %d\n", argv[1], size);
    
    bufp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (bufp < 0)
    {
        printf("mmap error\n");
        exit(1);
    }
    printf("bufp is %p\n", bufp);

    fwrite(bufp, size, 1, stdout);

    return 0;
}
